<template>
  <div class="workflow-page">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left"></span>
        返回首页
      </button>
      <h1>工作流设置</h1>
    </div>
    
    <div class="page-content">
      <div class="config-header">
        <p>配置和管理工单处理流程，自定义工单状态流转规则</p>
        <button class="primary-btn" @click="showAddWorkflowModal = true">
          <i class="icon-plus"></i> 新增工作流
        </button>
      </div>
      
      <div class="card">
        <div class="workflow-list">
          <div class="workflow-item" v-for="workflow in workflows" :key="workflow.id">
            <div class="workflow-header">
              <div class="workflow-info">
                <h3>{{ workflow.name }}</h3>
                <p class="workflow-desc">{{ workflow.description }}</p>
              </div>
              <div class="workflow-actions">
                <button class="edit-btn" @click="editWorkflow(workflow)">编辑</button>
                <button class="delete-btn" @click="deleteWorkflow(workflow.id)">删除</button>
                <label class="switch">
                  <input 
                    type="checkbox" 
                    v-model="workflow.active"
                    @change="toggleWorkflowStatus(workflow.id)"
                  >
                  <span class="slider round"></span>
                </label>
              </div>
            </div>
            
            <div class="workflow-steps">
              <div class="step" v-for="(step, index) in workflow.steps" :key="step.id">
                <div class="step-icon">
                  {{ index + 1 }}
                </div>
                <div class="step-content">
                  <h4>{{ step.name }}</h4>
                  <p class="step-desc">{{ step.description }}</p>
                  <div class="step-actions">
                    <span class="step-action" v-for="action in step.actions" :key="action.id">
                      {{ action.name }}
                    </span>
                  </div>
                </div>
                <div class="step-arrow" v-if="index < workflow.steps.length - 1">
                  <i class="icon-arrow-right"></i>
                </div>
              </div>
            </div>
          </div>
          
          <div class="empty-state" v-if="workflows.length === 0">
            <i class="icon-empty"></i>
            <p>暂无工作流，请点击新增按钮创建</p>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 新增/编辑工作流模态框 -->
    <div class="modal-overlay" v-if="showAddWorkflowModal || showEditWorkflowModal">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ showEditWorkflowModal ? '编辑工作流' : '新增工作流' }}</h3>
          <button class="close-btn" @click="closeWorkflowModal">&times;</button>
        </div>
        <div class="modal-body">
          <form class="workflow-form">
            <div class="form-group">
              <label>工作流名称 *</label>
              <input 
                type="text" 
                v-model="currentWorkflow.name" 
                class="form-control"
                required
              >
            </div>
            <div class="form-group">
              <label>描述</label>
              <textarea 
                v-model="currentWorkflow.description" 
                class="form-control"
                rows="3"
              ></textarea>
            </div>
            
            <div class="steps-section">
              <div class="steps-header">
                <h4>工作流步骤</h4>
                <button 
                  type="button" 
                  class="add-step-btn"
                  @click="addStep"
                >
                  <i class="icon-plus"></i> 添加步骤
                </button>
              </div>
              
              <div class="step-item" v-for="(step, index) in currentWorkflow.steps" :key="step.id">
                <div class="step-header">
                  <h5>步骤 {{ index + 1 }}</h5>
                  <button 
                    type="button" 
                    class="remove-step-btn"
                    @click="removeStep(index)"
                    v-if="currentWorkflow.steps.length > 1"
                  >
                    删除
                  </button>
                </div>
                
                <div class="form-group">
                  <label>步骤名称 *</label>
                  <input 
                    type="text" 
                    v-model="step.name" 
                    class="form-control"
                    required
                  >
                </div>
                
                <div class="form-group">
                  <label>步骤描述</label>
                  <textarea 
                    v-model="step.description" 
                    class="form-control"
                    rows="2"
                  ></textarea>
                </div>
                
                <div class="form-group">
                  <label>可执行操作</label>
                  <div class="step-actions-input">
                    <input 
                      type="text" 
                      v-model="newActionName" 
                      class="form-control"
                      placeholder="输入操作名称"
                    >
                    <button 
                      type="button" 
                      class="add-action-btn"
                      @click="addAction(step.id)"
                      :disabled="!newActionName"
                    >
                      添加
                    </button>
                  </div>
                  
                  <div class="actions-list" v-if="step.actions.length > 0">
                    <div class="action-tag" v-for="(action, aIndex) in step.actions" :key="action.id">
                      {{ action.name }}
                      <button 
                        type="button" 
                        class="remove-action-btn"
                        @click="removeAction(step.id, aIndex)"
                      >
                        &times;
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button class="cancel-btn" @click="closeWorkflowModal">取消</button>
          <button class="primary-btn" @click="saveWorkflow">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'WorkflowSetting',
  data() {
    return {
      showAddWorkflowModal: false,
      showEditWorkflowModal: false,
      newActionName: '',
      currentWorkflow: {
        id: null,
        name: '',
        description: '',
        active: true,
        steps: [
          {
            id: 1,
            name: '',
            description: '',
            actions: []
          }
        ]
      },
      workflows: [
        {
          id: 1,
          name: '标准工单流程',
          description: '适用于大多数常规工单的处理流程',
          active: true,
          steps: [
            {
              id: 101,
              name: '工单提交',
              description: '用户提交新工单',
              actions: [
                { id: 1001, name: '分配处理人' },
                { id: 1002, name: '直接关闭' }
              ]
            },
            {
              id: 102,
              name: '处理中',
              description: '工程师正在处理工单',
              actions: [
                { id: 1003, name: '标记为已解决' },
                { id: 1004, name: '转派给他人' },
                { id: 1005, name: '请求更多信息' }
              ]
            },
            {
              id: 103,
              name: '已解决',
              description: '工单问题已解决',
              actions: [
                { id: 1006, name: '关闭工单' },
                { id: 1007, name: '重新打开' }
              ]
            }
          ]
        },
        {
          id: 2,
          name: '紧急故障处理流程',
          description: '适用于紧急故障的快速响应流程',
          active: true,
          steps: [
            {
              id: 201,
              name: '紧急响应',
              description: '接到紧急故障报告',
              actions: [
                { id: 2001, name: '升级处理' },
                { id: 2002, name: '指派专家' }
              ]
            },
            {
              id: 202,
              name: '故障修复',
              description: '正在进行故障修复',
              actions: [
                { id: 2003, name: '确认修复' },
                { id: 2004, name: '需要更多资源' }
              ]
            },
            {
              id: 203,
              name: '事后分析',
              description: '故障解决后的原因分析',
              actions: [
                { id: 2005, name: '生成报告' },
                { id: 2006, name: '关闭流程' }
              ]
            }
          ]
        }
      ]
    };
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    editWorkflow(workflow) {
      this.currentWorkflow = JSON.parse(JSON.stringify(workflow));
      this.showEditWorkflowModal = true;
    },
    deleteWorkflow(id) {
      if (confirm('确定要删除这个工作流吗？')) {
        this.workflows = this.workflows.filter(wf => wf.id !== id);
      }
    },
    toggleWorkflowStatus(id) {
      const workflow = this.workflows.find(wf => wf.id === id);
      if (workflow) {
        workflow.active = !workflow.active;
      }
    },
    closeWorkflowModal() {
      this.showAddWorkflowModal = false;
      this.showEditWorkflowModal = false;
      this.resetCurrentWorkflow();
      this.newActionName = '';
    },
    resetCurrentWorkflow() {
      this.currentWorkflow = {
        id: null,
        name: '',
        description: '',
        active: true,
        steps: [
          {
            id: Date.now(),
            name: '',
            description: '',
            actions: []
          }
        ]
      };
    },
    addStep() {
      this.currentWorkflow.steps.push({
        id: Date.now() + Math.random(),
        name: '',
        description: '',
        actions: []
      });
    },
    removeStep(index) {
      this.currentWorkflow.steps.splice(index, 1);
    },
    addAction(stepId) {
      if (!this.newActionName.trim()) return;
      
      const step = this.currentWorkflow.steps.find(s => s.id === stepId);
      if (step) {
        step.actions.push({
          id: Date.now() + Math.random(),
          name: this.newActionName.trim()
        });
        this.newActionName = '';
      }
    },
    removeAction(stepId, actionIndex) {
      const step = this.currentWorkflow.steps.find(s => s.id === stepId);
      if (step) {
        step.actions.splice(actionIndex, 1);
      }
    },
    saveWorkflow() {
      // 简单验证
      if (!this.currentWorkflow.name.trim()) {
        alert('请输入工作流名称');
        return;
      }
      
      const hasInvalidStep = this.currentWorkflow.steps.some(step => 
        !step.name.trim()
      );
      
      if (hasInvalidStep) {
        alert('请确保所有步骤都填写了名称');
        return;
      }
      
      if (this.showEditWorkflowModal) {
        // 更新现有工作流
        const index = this.workflows.findIndex(wf => wf.id === this.currentWorkflow.id);
        if (index !== -1) {
          this.workflows.splice(index, 1, JSON.parse(JSON.stringify(this.currentWorkflow)));
        }
      } else {
        // 添加新工作流
        this.currentWorkflow.id = Date.now();
        this.workflows.push(JSON.parse(JSON.stringify(this.currentWorkflow)));
      }
      
      this.closeWorkflowModal();
    }
  }
}
</script>

<style scoped>
.workflow-page {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e1e5e8;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.page-content {
  margin-top: 20px;
}

.config-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.config-header p {
  color: #666;
  font-size: 14px;
}

.card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 20px;
}

.workflow-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.workflow-item {
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.2s;
}

.workflow-item:hover {
  box-shadow: 0 3px 15px rgba(0, 0, 0, 0.07);
}

.workflow-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background-color: #f9fafb;
  border-bottom: 1px solid #f0f0f0;
}

.workflow-info h3 {
  margin: 0 0 5px 0;
  font-size: 16px;
}

.workflow-desc {
  margin: 0;
  font-size: 13px;
  color: #666;
}

.workflow-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.edit-btn, .delete-btn {
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.2s;
  border: none;
}

.edit-btn {
  background-color: #e3f2fd;
  color: #1976d2;
}

.edit-btn:hover {
  background-color: #bbdefb;
}

.delete-btn {
  background-color: #ffebee;
  color: #d32f2f;
}

.delete-btn:hover {
  background-color: #ffcdd2;
}

/* Switch styling */
.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 20px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: .4s;
}

input:checked + .slider {
  background-color: #3498db;
}

input:checked + .slider:before {
  transform: translateX(20px);
}

.slider.round {
  border-radius: 20px;
}

.slider.round:before {
  border-radius: 50%;
}

.workflow-steps {
  padding: 20px;
  display: flex;
  flex-wrap: wrap;
  position: relative;
}

.workflow-steps::before {
  content: '';
  position: absolute;
  top: 30px;
  left: 60px;
  right: 60px;
  height: 2px;
  background-color: #e0e0e0;
  z-index: 1;
}

.step {
  display: flex;
  align-items: center;
  position: relative;
  z-index: 2;
  padding: 0 15px;
  min-width: 200px;
}

.step-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #3498db;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-right: 10px;
  flex-shrink: 0;
}

.step-content {
  flex-grow: 1;
}

.step-content h4 {
  margin: 0 0 5px 0;
  font-size: 14px;
}

.step-desc {
  margin: 0 0 8px 0;
  font-size: 12px;
  color: #666;
}

.step-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.step-action {
  font-size: 11px;
  padding: 2px 8px;
  background-color: #f0f5ff;
  color: #1976d2;
  border-radius: 4px;
}

.step-arrow {
  color: #ccc;
  margin: 0 5px;
}

.icon-arrow-right::before {
  content: "→";
}

.empty-state {
  text-align: center;
  padding: 40px 0;
  color: #999;
}

.icon-empty::before {
  content: "📊";
  font-size: 48px;
  display: block;
  margin-bottom: 15px;
}

.primary-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: 500;
}

.primary-btn:hover {
  background: #2980b9;
}

.icon-plus::before {
  content: "+";
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  border-radius: 10px;
  width: 700px;
  max-width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
}

.modal-body {
  padding: 20px;
}

.workflow-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-group label {
  font-weight: 500;
  font-size: 14px;
}

.form-control {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
}

.steps-section {
  margin-top: 10px;
}

.steps-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.add-step-btn {
  padding: 5px 10px;
  background-color: #e3f2fd;
  color: #1976d2;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.step-item {
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 15px;
  background-color: #f9fafb;
}

.step-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.step-header h5 {
  margin: 0;
  font-size: 14px;
  color: #333;
}

.remove-step-btn {
  background: none;
  border: none;
  color: #d32f2f;
  font-size: 12px;
  cursor: pointer;
  padding: 3px 5px;
}

.step-actions-input {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.step-actions-input .form-control {
  flex-grow: 1;
}

.add-action-btn {
  padding: 0 12px;
  background-color: #e8f5e9;
  color: #388e3c;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}

.add-action-btn:disabled {
  background-color: #f5f5f5;
  color: #bdbdbd;
  cursor: not-allowed;
}

.actions-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.action-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  background-color: #f0f5ff;
  color: #1976d2;
  border-radius: 4px;
  font-size: 12px;
}

.remove-action-btn {
  background: none;
  border: none;
  color: #1976d2;
  font-size: 12px;
  cursor: pointer;
  padding: 0 2px;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn {
  padding: 8px 15px;
  background: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.2s;
}

.cancel-btn:hover {
  background: #e9e9e9;
}

.icon-arrow-left::before {
  content: "←";
}
</style>